package com.example.onlinemusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.onlinemusic.common.R;
import com.example.onlinemusic.mapper.AdminMapper;
import com.example.onlinemusic.model.domain.Admin;
import com.example.onlinemusic.model.request.AdminRequest;
import com.example.onlinemusic.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * Admin接口实现类
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Override
    public R verityPasswd(AdminRequest adminRequest, HttpSession session) {
        // 具体校验账号和密码的业务
        // 调用Mapper去完成基础的查询功能
        // 设置查询条件: select * from admin where name='aaa' and password = 'bbb';
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", adminRequest.getUsername());
        queryWrapper.eq("password", adminRequest.getPassword());
        // 得到查询的数量
        Long count = adminMapper.selectCount(queryWrapper);
        if (count > 0) {// 登陆成功
            session.setAttribute("name", adminRequest.getUsername());
            return R.success("登录成功");
        } else {// 登陆失败
            return R.error("用户名或者密码失败");
        }
    }


}
